package org.NooLab.itexx.storage;

import java.io.File;

import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.strings.StringsUtil;

public class DatabaseMgmt {

	public static final int _BASEDIR_QUERY_APPMAIN = 1;
	public static final int _BASEDIR_QUERY_PROJECT = 2;
	public static final int _BASEDIR_QUERY_STOR    = 3;
	
	
	public DatabaseMgmt(){
		
	}
	
	
	/** this is indeed global, across ALL applications, so we have to use a global dir inside iTexx
		storage is on the same level as the main applications sub dir : [any path]/iTexx/storage !
	 *
	 *	
	*/ 
	public static String setH2BaseDir( String queryDir, int foldertype ) throws Exception{
		
		String _abspath,requestedDir ="" ;
		File _fil;
		
		if (DFutils.folderExists(queryDir)==false){
			throw(new Exception("the folder provided to setH2BaseDir() does not exist .\n"+ 
					            "   "+queryDir ));
		}
		
		
		_fil = new File(queryDir) ;
		_abspath = _fil.getAbsolutePath() ;
		
		_abspath = StringsUtil.replaceall(_abspath, "\\", "/") ;
		queryDir = StringsUtil.replaceall(queryDir, "\\", "/") ;
		
		if (_abspath.contains("/iTexx/")==false){
			throw(new Exception("The prepared folder is not an iTexx application folder.\n"+
        						" - supplied : "+ queryDir +" \n"+ 
        						" - prepared : "+_abspath+" \n"
					            
			                    ));
		}
		
		if (foldertype<1){
			foldertype=1;
		}
		if (foldertype>3){
			foldertype=3;
		}
		
		requestedDir = _abspath;
		// for (int i=1;i<=foldertype;i++){
			requestedDir = DFutils.getParentDir(_abspath, foldertype) ;
			
		
		System.setProperty("h2.baseDir", requestedDir);  // 
		return requestedDir;
	}
	
	
}
